<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/20
 * Time: 10:32
 */
/**
 * 投资回调接口
 */
function investCallback($id){
    define(CLIENTSECRET, '9db53d8e744742158ea360d24c049ddf');//生产环境
    $id = intval($id);
    if(empty($id)) {
        $res['retNo'] = '000';
        $res['msg'] = "投资回调失败，第三方存管更新失败";
        return json_encode($res);
    }
    $sql = "SELECT a.id as id,b.mobile as phone, a.create_time as datetime,a.deal_id as pid,a.money as InvestAmount,c.repay_time as term,c.repay_time_type,c.rate,b.xicai_id as userid ".
        " from fanwe_deal_load a ".
        " LEFT JOIN fanwe_user b on a.user_id=b.id ".
        " LEFT JOIN fanwe_deal c on a.deal_id=c.id ".
        " WHERE a.id=".$id;
    $dataToXcw = $GLOBALS['db']->getRow($sql);
    if(empty($dataToXcw)) {
        $res['retNo'] = '000';
        $res['msg'] = '投标记录有误';
        return json_encode($res);
    }
    if($dataToXcw['repay_time_type'] == '1') {
        $dataToXcw['term'] *= 30;
    }
    $dataToXcw['display'] = 'pc';
    $dataToXcw['Earnings'] = '0';
    $dataToXcw['url'] = APP_ROOT_PATH.'index.php?ctl=deal&id='.$dataToXcw['pid'];   //url为投资的p2p网贷平台产品地址
    $dataToXcw['commision'] = comToXcw($dataToXcw['id']);

//    $invest = array("id" => 1,  //其中id为本次投资的唯一编号
//        "commision" => 10,      //Commision为p2p网贷平台返佣给希财网的金额
//        "datetime" => "2015-01-01 12:00",   //datetime为投资时间
//        "earnings" => 100,      //Earnings用户投资收益（若无请填0）
//        "investamount" => 10000,        //InvestAmount用户投资金额
//        "phone" => "13800138000",       //phone为用户手机
////            "term"=>7,                      //可选，term 投资期限（以天为单位）
////            "rate"=>19.2,               //可选，rate 产品利率
////            "display"=>'pc',            //可选，display为用户来源，默认值为pc（pc为电脑访问，mobile为手机访问）
////            "userid"=>12,               //可选，userid为用户在希财网注册时的编号，该编号在自动注册时会作为参数传过去
//        "pid" => "1001",        //pid为投资的p2p网贷平台产品id
//        "url" => "http://www.p2p.com/product/1.aspx?s=csai" //url为投资的p2p网贷平台产品地址
//    );

    $result = InvestCallBack2($dataToXcw);
    $result = json_decode($result, true);
    if($result['code'] == '0'){
        echo "希财网投资回调成功";
    } else {
        echo "希财网投资回调失败，原因：" .$result['ErrorMessage'];
    }
}
function InvestCallBack2($arr){

    $param = "commision=".$arr['commision'].
        "&datetime=".$arr['datetime'].
        "&earnings=".$arr['earnings'].
        "&id=".$arr['id'].
        "&investamount=".$arr['investamount'].
        "&phone=".$arr['phone'].
        "&pid=".$arr['pid'].
        "&url=".$arr['url'];
    $sign = encode($param,substr(CLIENTSECRET,0,8));
    $result = curl_post("http://api.csai.cn/apitest/InvestCallBack",array("client_id"=>CLIENTID,"sign"=>$sign));
    return $result;
}

//取得返佣
function comToXcw($pid){
    $id = intval($pid);
    if(empty($id)) {
        return 0;
    }
    return 0;
}

function encode($text,$key){
    return base64_encode(mcrypt_encrypt(MCRYPT_DES, $key, pkcs5Pad($text, mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC)), MCRYPT_MODE_CBC, $key));
}
function curl_post($url, $post) {
    if(is_string($post)){
        parse_str($post,$post);
    }
    if(!is_array($post)){
        return "";
    }
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER         => false,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $post,
    );
    $ch = curl_init($url);
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
//        dump($result);exit;
    curl_close($ch);
    return $result;
}
function pkcs5Pad($text, $blocksize) {
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
}